<!-- 渐变柱状图 -->
<template>
  <div ref="chartRef" :style="{ width, height }"></div>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';
  import echarts from '/@/utils/lib/echarts';
  import { useECharts } from '/@/hooks/web/useECharts';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '300px',
    },

    seriesVal: {
      type: Object,
      default: () => {},
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      const {Xdata , seriesValue} = props.seriesVal
      if (seriesValue && seriesValue.length === 0) {
        setOptions({
          title: {
            textStyle: {
              color: 'grey',
              fontSize: 20,
            },
            text: '暂无数据',
            left: 'center',
            top: 'center',
          },
        });
      } else {
        setOptions({
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            },
            formatter: params => {
              const {
                  name,
                  data,
              } = params[0];
              return `
                  <span style="font-size: 14px;font-family: Source Han Sans CN-Medium;font-weight: 500;color: #FFFFFF;margin-bottom:12px;">${name}</span>
                  <sapn style="font-size: 14px;font-family: Source Han Sans CN-Medium;font-weight: 500;color: #FFFFFF;margin-bottom:4px;">：${data} 次</span>
              `
            },
            extraCssText: 'opacity: 0.8;background-color:#050F1B;padding:16px;box-shadow: 1px 6px 15px 1px rgba(0,0,0,0.13);border-radius: 4px;filter: blur(undefinedpx);border:none;'
          },
          grid: {
            top: 10,
            left: 10,
            right: 20,
            bottom: 0,
            containLabel: true,
          },
          xAxis: {
            type: 'category',
            axisTick: {
              show: false //隐藏X轴刻度
            },
            axisLine: {
              lineStyle: {
                  color: "#CCCCCC"
              }
            },
            axisLabel: {
              show: true,
            },
            data: Xdata
          },
          yAxis: {
            type: 'value',
            name: '',
            
          },
          series: [
            {
                name: '',
                type: 'bar',
                showBackground: true,
                barWidth: 30,
                barGap: 10,
                backgroundStyle: {
                    color: "transparent"
                },
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#4B87FFFF" },
                    { offset: 1, color: "#82BCFFFF" }
                    ])
                },
                data: seriesValue,
            },
          ],
          ...props.seriesVal,
        });
      }
    },
    { immediate: true },
  );
</script>
